package top.hzy520.shopcart.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.hzy520.common.constants.Constants;
import top.hzy520.common.resp.ApiResult;
import top.hzy520.common.rest.BaseController;
import top.hzy520.shopcart.dto.ProductSkuInputDto;
import top.hzy520.shopcart.dto.ShopCartOutputDto;
import top.hzy520.shopcart.service.ShopCartService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * @Author: HouZeYu
 * @Description: 购物车控制器
 * @Date: Created in 20:43 2018/6/12
 */
@RestController
@RequestMapping("/shopcart")
public class ShopCartController extends BaseController {

    @Autowired
    @Qualifier("shopCartServiceImpl")
    private ShopCartService shopCartService;
     /**
     *@Author: HouZeYu
     *@Description: 查询购物车列表
     *@Date: 20:54 2018/6/12
     */
    @RequestMapping("/list")
    public ApiResult shopCartList(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
        ShopCartOutputDto cartOutputDto=shopCartService.shopCartList(getIpFromRequest(request),getCurrentUser(request),request);
            ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"查询成功");
            apiResult.setData(cartOutputDto);
              return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description: 添加购物车
    *@Date: 8:52 2018/6/13
    */
    @RequestMapping("/addshopcart")
    public ApiResult addShopCart(@RequestBody List<ProductSkuInputDto> skuInputDtoList, HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
     ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"添加成功");
    ShopCartOutputDto skuOutputDto=shopCartService.addShopCart(getCurrentUser(request),getIpFromRequest(request),skuInputDtoList,request,response);
      apiResult.setData(skuOutputDto);
      return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description: 删除购物车商品
    *@Date: 15:56 2018/6/19
    */
    @RequestMapping("/delshopcart")
    public ApiResult delShopCart(@RequestBody List<ProductSkuInputDto> skuInputDtoList,HttpServletResponse response,HttpServletRequest request) throws UnsupportedEncodingException {
      ShopCartOutputDto shopCartOutputDto=shopCartService.delShopCart(skuInputDtoList,getIpFromRequest(request),getCurrentUser(request),request,response);
     ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"移除成功");
     apiResult.setData(shopCartOutputDto);
      return apiResult;
    }
    /**
    *@Author: HouZeYu
    *@Description: 修改商品选择状态
    *@Date: 14:43 2018/7/2
    */
    @RequestMapping("/chooseshopcart")
    public ApiResult chooseShopcart(@RequestBody List<ProductSkuInputDto> skuInputDtoList,HttpServletResponse response,HttpServletRequest request) throws UnsupportedEncodingException {
        ShopCartOutputDto shopCartOutputDto=shopCartService.chooseShopcart(skuInputDtoList,getIpFromRequest(request),getCurrentUser(request),request,response);
        ApiResult apiResult=new ApiResult(Constants.RESP_STATUS_OK,"选择成功");
        apiResult.setData(shopCartOutputDto);
        return apiResult;
    }
}
